package com.sansan.qiangji.controller.base;

import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.base.ProductTypeAddForm;
import com.sansan.qiangji.form.base.ProductTypeEditForm;
import com.sansan.qiangji.form.base.ProductTypeListForm;
import com.sansan.qiangji.realize.base.ProductTypeReal;
import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zhaobc
 * @description
 * @date 2020-11-16
 */
@CrossOrigin()
@RestController
@RequestMapping("/productType")
@Slf4j
public class ProductTypeController {

    @Autowired
    private ProductTypeReal productTypeReal;

    @PostMapping("/getproductTypeList")
    public ResultVO<Object> getproductTypeList(@Valid @RequestBody ProductTypeListForm listForm){
        return ResultVOUtil.success(productTypeReal.getproductTypeList(listForm));
    }

    @PostMapping("/editProductType")
    public ResultVO<Object> editProductType(@Valid @RequestBody ProductTypeEditForm productTypeEditForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【物品类型】参数不正确，editProductType={}",productTypeEditForm);
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return productTypeReal.editProductType(productTypeEditForm);
    }
    @PostMapping("/addProductType")
    public ResultVO<Object> addProductType(@Valid @RequestBody ProductTypeAddForm productTypeAddForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【物品类型】参数不正确，editProductType={}",productTypeAddForm);
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return productTypeReal.addProductType(productTypeAddForm);
    }
    @PostMapping("/deleteProductType")
    public ResultVO<Object> deleteProductType(@RequestParam(value = "id") Integer id){
        return productTypeReal.deleteProductType(id);
    }
}
